//
//  LzwHomeViewController.m
//  NewHalfsugar
//
//  Created by 刘志武 on 15/12/31.
//  Copyright © 2015年 刘志武. All rights reserved.
//

#import "LzwHomeViewController.h"
#import "Block.h"
#import "UIImageView+WebCache.h"
#import "LzwBaseImageView.h"
#import "LzwBaseScrollView.h"
#import "LzwBaseButton.h"

#import "LzwNewViewController.h"
#import "LzwNewYearViewController.h"
#import "LzwArtViewController.h"
#import "LzwBeautifulViewController.h"
#import "LzwDeviseViewController.h"
#import "LzwEatViewController.h"
#import "LzwFestuvalViewController.h"
#import "LzwGiftViewController.h"
#import "LzwHandBookViewController.h"
#import "LzwHostelViewController.h"
#import "LzwKitchenViewController.h"
#import "LzwPartViewController.h"
#import "LzwStudentViewController.h"
#import "LzwStyleViewController.h"
#import "LzwWorkingViewController.h"

#import "LzwSearchController.h"
#import "LzwSingController.h"


static CGFloat const titleH = 44;
static CGFloat const navBarH = 64;
static CGFloat const maxTitleScale = 1.3;

#define YCKScreenW [UIScreen mainScreen].bounds.size.width
#define YCKScreenH [UIScreen mainScreen].bounds.size.height

@interface LzwHomeViewController ()<UIScrollViewDelegate>


@property (nonatomic, retain) LzwBaseScrollView *titleScrollView;
@property (nonatomic, retain) LzwBaseScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, retain) LzwBaseButton *selTitleButton;

@property (nonatomic, strong) NSMutableArray *buttons;


@end

@implementation LzwHomeViewController


- (NSMutableArray *)buttons
{
    if (!_buttons)
    {
        self.buttons = [NSMutableArray array];
    }
    return _buttons;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupTitleScrollView];//创建小scrollview
    [self setupContentScrollView];//创建大scrollview
    
    [self addChildViewController];//添加子控制器
    [self setupTitle];//根据子视图个数创建小scrollview的按钮
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * YCKScreenW, 0);
    self.contentScrollView.bounces =NO;
    
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    
    
    //设置导航栏title
    self.navigationItem.title = @"梦Mall";
    
    //设置返回按钮
   
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     
     
     @{NSFontAttributeName:[UIFont systemFontOfSize:20 * WIDTH],
       
       
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
     //设置左侧分类按钮
    UIImage *image1 = [UIImage imageNamed:@"fenlei.png"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image1 style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    //设置右侧日历按钮
    UIImage *image2 = [UIImage imageNamed:@"rili.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    
    
    
    
}
//左侧分类按钮
- (void)leftBarButtonItemAction:(UIBarButtonItem *)button{
    
    LzwSearchController *search = [[LzwSearchController alloc]init];
    
    [self.navigationController pushViewController:search animated:YES];
    
    
}
//右侧日历按钮
- (void)rightBarButtonItemAction:(UIBarButtonItem *)but{
    
    LzwSingController *sing = [[LzwSingController alloc]init];
    
    [self.navigationController pushViewController:sing animated:YES];
    
}


#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
//    CGFloat y = self.navigationController ? navBarH : 0;
    CGRect rect = CGRectMake(0, 0, YCKScreenW, titleH);
    
    LzwBaseScrollView *titleScrollView = [[LzwBaseScrollView alloc] initWithFrame:rect];
//    titleScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
}

#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, YCKScreenW, YCKScreenH - navBarH);
    
    LzwBaseScrollView *contentScrollView = [[LzwBaseScrollView alloc] initWithFrame:rect];
    //    contentScrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
}

#pragma mark - 添加子控制器
- (void)addChildViewController
{
   
    LzwNewViewController *vc = [[LzwNewViewController alloc] init];
        vc.title = @"最新";
        [self addChildViewController:vc];
    
        LzwNewYearViewController *vc1 = [[LzwNewYearViewController alloc] init];
        vc1.title = @"New Year!";
        [self addChildViewController:vc1];
    
        LzwArtViewController *vc2 = [[LzwArtViewController alloc] init];
        vc2.title = @"文艺";
        [self addChildViewController:vc2];
    
        LzwGiftViewController *vc3 = [[LzwGiftViewController alloc] init];
        vc3.title = @"礼物";
        [self addChildViewController:vc3];
    
        LzwHandBookViewController *vc4 = [[LzwHandBookViewController alloc] init];
        vc4.title = @"指南";
        [self addChildViewController:vc4];
    
        LzwBeautifulViewController *vc5 = [[LzwBeautifulViewController alloc] init];
        vc5.title = @"爱美";
        [self addChildViewController:vc5];
    
        LzwDeviseViewController *vc6 = [[LzwDeviseViewController alloc] init];
        vc6.title = @"设计";
        [self addChildViewController:vc6];
    
        LzwEatViewController *vc7 = [[LzwEatViewController alloc] init];
        vc7.title = @"吃货";
        [self addChildViewController:vc7];
    
        LzwStyleViewController *vc8 = [[LzwStyleViewController alloc] init];
        vc8.title = @"格调";
        [self addChildViewController:vc8];
    
    
        LzwKitchenViewController *vc9 = [[LzwKitchenViewController alloc] init];
        vc9.title = @"厨房";
        [self addChildViewController:vc9];
    
        LzwWorkingViewController *vc10 = [[LzwWorkingViewController alloc] init];
        vc10.title = @"上班";
        [self addChildViewController:vc10];
    
        LzwStudentViewController *vc11 = [[LzwStudentViewController alloc] init];
        vc11.title = @"学生党";
        [self addChildViewController:vc11];
    
        LzwPartViewController *vc12 = [[LzwPartViewController alloc] init];
        vc12.title = @"聚会";
        [self addChildViewController:vc12];
    
        LzwFestuvalViewController *vc13 = [[LzwFestuvalViewController alloc] init];
        vc13.title = @"节日";
        [self addChildViewController:vc13];
    
        LzwHostelViewController *vc14 = [[LzwHostelViewController alloc] init];
        vc14.title = @"宿舍";
        [self addChildViewController:vc14];
    

    
    
}

#pragma mark - 设置标题
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 100 * WIDTH;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        LzwBaseButton *btn = [[LzwBaseButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
        
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

// 按钮点击
- (void)chick:(LzwBaseButton *)btn
{
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * YCKScreenW;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}
// 选中按钮
- (void)selTitleBtn:(LzwBaseButton *)btn
{
    [self.selTitleButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    self.selTitleButton = btn;
    [self setupTitleCenter:btn];
}

- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * YCKScreenW;
    
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, YCKScreenW, YCKScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}

- (void)setupTitleCenter:(LzwBaseButton *)btn
{
    CGFloat offset = btn.center.x - YCKScreenW * 0.5* WIDTH;
    
    NSLog(@"%lf", offset);
    
    if (offset < 0)
    {
        offset = 0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - YCKScreenW;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(LzwBaseScrollView *)scrollView
{
    NSUInteger i = self.contentScrollView.contentOffset.x / YCKScreenW;
    [self selTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}

// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(LzwBaseScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger leftIndex = offsetX / YCKScreenW;
    NSInteger rightIndex = leftIndex + 1;
    
    //    NSLog(@"%zd,%zd",leftIndex,rightIndex);
    
    LzwBaseButton *leftButton = self.buttons[leftIndex];
    
    LzwBaseButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    CGFloat scaleR = offsetX / YCKScreenW - leftIndex;
    
    CGFloat scaleL = 1 - scaleR;
    
    
    CGFloat transScale = maxTitleScale - 1;
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
    
//    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *rightColor = [UIColor redColor];
//    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor orangeColor];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
